package com.atguigu.tingshu.order.api;

import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order")
@SuppressWarnings({"all"})
public class OrderInfoApiController {

	@Autowired
	private OrderInfoService orderInfoService;

	/**
	 * 订单确认
	 * @param tradeVo
	 * @return
	 */
	@PostMapping("/orderInfo/trade")
	@GuiGuLogin
	public Result<OrderInfoVo> tradeOrderData(@RequestBody TradeVo tradeVo){
		OrderInfoVo orderInfoVo = orderInfoService.tradeOrderData(tradeVo);
		return Result.ok(orderInfoVo);
	}

	/**
	 * 提交订单
	 * @param orderInfo
	 * @return
	 */
	@PostMapping("/orderInfo/submitOrder")
	@GuiGuLogin
	public Result<Map<String,String>> submitOrder(@RequestBody OrderInfoVo orderInfoVo){
		Map<String,String> orderNo = orderInfoService.submitOrder(orderInfoVo);
		return Result.ok(orderNo);
	}

	/**
	 * 根据订单号获取订单信息
	 * @param orderNo
	 * @return
	 */
	@GetMapping("/orderInfo/getOrderInfo/{orderNo}")
	@GuiGuLogin
	public Result<OrderInfo> getOrderInfo(@PathVariable String orderNo){
		OrderInfo orderInfo = orderInfoService.getOrderInfo(orderNo);
		return Result.ok(orderInfo);
	}

	/**
	 * 分页查询用户订单
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@GetMapping("/orderInfo/findUserPage/{page}/{pageSize}")
	@GuiGuLogin
	public Result<Page<OrderInfo>> findUserPage(@PathVariable Long page,@PathVariable Long pageSize){

		Page<OrderInfo> orderInfoPage = new Page<>(page, pageSize);

		OrderItem orderItem = new OrderItem();
		orderItem.setColumn("create_time");
		orderItem.setAsc(false);
		orderInfoPage.addOrder(orderItem);

		Page<OrderInfo> infoPage = orderInfoService.findUserPage(orderInfoPage);
		return Result.ok(infoPage);
	}

}



















